package com.example.liaotian.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.liaotian.Bean.User;
import com.example.liaotian.R;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 个人信息界面
 */
public class MyMessageActivity extends AppCompatActivity {
    EditText nike,poss;
    Button nikeyes,possyes;
    TextView nikename,possword,usename;
    BmobUser user = BmobUser.getCurrentUser();
    final String id = user.getObjectId();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_message);

        initView();
        initClick();
        getMyInfo();
    }

    public void refresh() {
        onCreate(null);
    }

    /**
     * 加载个人信息
     */
    private void getMyInfo() {

        //通过当前登录用户的id找到用户信息并显示
        BmobQuery<User> bmobQuery = new BmobQuery<>();

        bmobQuery.getObject(id, new QueryListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if (e == null){

                    nikename.setText(user.getNickName());
                    usename.setText(user.getUsername());
                }else {
                    Toast.makeText(MyMessageActivity.this,"加载失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initClick() {
        /**
         * 点击昵称会隐藏昵称然后输入框和确定按钮出现
         */
        nikename.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                nike.setVisibility(View.VISIBLE);
                nikeyes.setVisibility(View.VISIBLE);
                nikename.setVisibility(View.GONE);
            }
        });

        /**
         * 按下确定按钮获得输入框的内容然后先把昵称显示改成新的昵称
         * 然后获取到当前登陆的账号的id进行更改
         */
        nikeyes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             if (nike.getText().toString().isEmpty()){
                 Toast.makeText(MyMessageActivity.this,"请输入新昵称！",Toast.LENGTH_SHORT).show();
             }else {

                 nikename.setText(nike.getText().toString());
                 User u = new User();
                 u.setObjectId(id);
                 u.setNickName(nike.getText().toString());
                 u.update(new UpdateListener() {
                     @Override
                     public void done(BmobException e) {
                         if (e == null){

                             Toast.makeText(MyMessageActivity.this,"昵称修改成功！",Toast.LENGTH_SHORT).show();
                         }else {
                             Toast.makeText(MyMessageActivity.this,"昵称修改失败！"+e.getMessage(),Toast.LENGTH_SHORT).show();
                         }
                     }
                 });


                 nikename.setVisibility(View.VISIBLE);
                 nike.setVisibility(View.GONE);
                 nikeyes.setVisibility(View.GONE);
             }

            }
        });

        possword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                poss.setVisibility(View.VISIBLE);
                possyes.setVisibility(View.VISIBLE);
                possword.setVisibility(View.GONE);
            }
        });
        possyes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             if (poss.getText().toString().isEmpty()){
                 Toast.makeText(MyMessageActivity.this,"请输入新密码！",Toast.LENGTH_SHORT).show();
             }else {

                 user.setPassword(poss.getText().toString());
                 user.update(new UpdateListener() {
                     @Override
                     public void done(BmobException e) {
                         if (e == null){

                             Toast.makeText(MyMessageActivity.this,"密码修改成功！",Toast.LENGTH_SHORT).show();
                         }else {
                             Toast.makeText(MyMessageActivity.this,"密码修改失败！"+e.getMessage(),Toast.LENGTH_SHORT).show();
                         }
                     }
                 });





                 possword.setVisibility(View.VISIBLE);
                 poss.setVisibility(View.GONE);
                 possyes.setVisibility(View.GONE);
             }
            }
        });

    }

    private void initView() {

        nikename = findViewById(R.id.nikename);
        nike = findViewById(R.id.editnike);
        possword = findViewById(R.id.poss);
        poss = findViewById(R.id.editposs);
        nikeyes = findViewById(R.id.button);
        possyes = findViewById(R.id.button2);
        usename = findViewById(R.id.username);
    }
}
